﻿namespace dotnet_notes.Models
{
    /// <summary>
    /// 时间区间类
    /// </summary>
    public class TimeInterval
    {
        /// <summary>
        /// 天数
        /// </summary>
        public int Days { get; }

        /// <summary>
        /// 小时数
        /// </summary>
        public int Hours { get; }

        /// <summary>
        /// 分钟数
        /// </summary>
        public int Minutes { get; }

        /// <summary>
        /// 秒数
        /// </summary>
        public int Seconds { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="reducedTime">被减时间</param>
        /// <param name="subtractTime">减去时间</param>
        public TimeInterval(DateTime reducedTime, DateTime subtractTime)
        {
            if (reducedTime < subtractTime)
            {
                throw new InvalidOperationException("被减时间不应比减去时间小");
            }

            var interval = reducedTime - subtractTime;

            Days = interval.Days;
            Hours = interval.Hours;
            Minutes = interval.Minutes;
            Seconds = interval.Seconds;
        }

        /// <summary>
        /// 时间间隔转文本
        /// </summary>
        public string ToText()
        {
            string text = Days > 0 ? Days + "天" : "";

            if (string.IsNullOrEmpty(text))
            {
                text += Hours > 0 ? Hours + "小时" : "";
            }
            else
            {
                text += Hours + "小时";
            }

            if (string.IsNullOrEmpty(text))
            {
                text += Minutes > 0 ? Minutes + "分" : "";
            }
            else
            {
                text += Minutes + "分";
            }

            return text += Seconds + "秒";
        }

        /// <summary>
        /// 获取时间间隔总分钟数
        /// </summary>
        public int GetTotalMinutes()
        {
            if (Days > 0)
            {
                throw new InvalidOperationException("时间间隔超过一天");
            }

            return Hours * 60 + Minutes;
        }
    }
}
